package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.WheelView.TimePickerView;
import com.haotunet.app.goldtreasure.db.DBOpenHelper;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.ExtractGold;
import com.haotunet.app.goldtreasure.model.GoldInfo;
import com.haotunet.app.goldtreasure.model.OrderInfo;
import com.haotunet.app.goldtreasure.model.StoreInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.ShareStore;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.TimeUtils;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.OrderPopupView;
import com.haotunet.app.goldtreasure.views.ReseverTimePopubView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class StorePickReserveAct extends BaseActivity {
    @InjectView(R.id.shop_image)
    ImageView shopImage;
    @InjectView(R.id.title_txt)
    TextView titleTxt;
    @InjectView(R.id.shop_name)
    TextView shopName;
    @InjectView(R.id.shop_address)
    TextView shopAddress;
    @InjectView(R.id.txt_contact_person)
    TextView txtContactPerson;
    @InjectView(R.id.txt_contact_phone)
    TextView txtContactPhone;
    @InjectView(R.id.tv_gold_gram)
    TextView tvGoldGram;
    @InjectView(R.id.tv_gold_type)
    TextView tvGoldType;
    @InjectView(R.id.tv_resever_time)
    TextView tvReseverTime;
    @InjectView(R.id.switch_btn_invoice)
    ImageView switchBtnInvoice;
    @InjectView(R.id.tip_txt1)
    TextView tipTxt1;
    @InjectView(R.id.root)
    RelativeLayout root;
    @InjectView(R.id.tv_invoice)
    TextView tvInvoice;
    private DBOpenHelper helper;
    private final int GET_EXTRACT_GOLD_TYPE_SUCCESS = 1;
    private final int SUBMIT_EXTRACT_RESEVER_INFO_SUCCESS = 2;
    private List<String> gold_type;
    private List<ExtractGold> gramList;
    private String reseverTime;
    private String goldType;
    private String isBill;
    private OrderInfo orderInfo;
    private boolean isInvoiceToogle;
    private int gram20, gram50, gram100, gram200, gram500, gram1000;
    TimePickerView pvTime;

    private Handler handle = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GET_EXTRACT_GOLD_TYPE_SUCCESS:
                    GoldInfo goldInfo = (GoldInfo) msg.obj;
                    gold_type = goldInfo.getGoldType();
                    goldType = gold_type.get(0);
                    tvGoldType.setText(gold_type.get(0));
                    tipTxt1.setText(goldInfo.getTips());
                    break;
                case SUBMIT_EXTRACT_RESEVER_INFO_SUCCESS:
                    orderInfo = (OrderInfo) msg.obj;
                    if (shareStore.getInt(Strs.IS_HAD_BANK_CARD, 0) == 0) {
                        ActivityUtils.gotoNewUserBuy(StorePickReserveAct.this, "3", orderInfo);
                    } else {
                        showOrderDetailView();
                    }
                    break;

                default:
                    break;
            }
        }

    };
    private String shopId;
    private StoreInfo info;
    private ShareStore shareStore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_pick_reserve);
        ButterKnife.inject(this);
        tintManager.setStatusBarAlpha(0f);
        helper = new DBOpenHelper(StorePickReserveAct.this);
        shareStore = new ShareStore(this);
        initView();
        initWheelTimeView();
        initData();
    }

    private void initWheelTimeView() {
        pvTime = new TimePickerView(this, TimePickerView.Type.ALL);
        pvTime.setTime(TimeUtils.getDateAfterTenToday(new Date()));
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        // 时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                tvReseverTime.setText(TimeUtils.getTime(date));
            }
        });
    }

    private void initView() {
        Intent intent = getIntent();
        info = (StoreInfo) intent.getSerializableExtra("storeDetailInfo");
        shopId = intent.getStringExtra("shopId");
        gram20 = intent.getIntExtra("gram20", 0);
        gram50 = intent.getIntExtra("gram50", 0);
        gram100 = intent.getIntExtra("gram100", 0);
        gram200 = intent.getIntExtra("gram200", 0);
        gram500 = intent.getIntExtra("gram500", 0);
        gram1000 = intent.getIntExtra("gram1000", 0);
        titleTxt.setText(getString(R.string.proposed_gold));
        Glide.with(StorePickReserveAct.this).load(info.getImage().trim()).placeholder(R.drawable.placeholder_shop_big).error(R.drawable.placeholder_shop_big).into(shopImage);
        shopName.setText(info.getShopName());
        shopAddress.setText(info.getAddress());
        txtContactPhone.setText(helper.getMobile());
        txtContactPerson.setText(helper.getRealName());
        isBill = "1";
        tvGoldGram.setText(
                gram20 * 20 + gram50 * 50 + gram100 * 100 + gram200 * 200 + gram500 * 500 + gram1000 * 1000 + "g");
    }

    private void initData() {
        ActionUtil actionUtil = new ActionUtil(StorePickReserveAct.this);
        actionUtil.getExtractGoldType();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                GoldInfo info = (GoldInfo) model;
                if (info == null) {
                    UIHelper.showToast(StorePickReserveAct.this, getString(R.string.rank_fail));
                    return;
                }
                if (info.getErrorMsg() != null) {
                    if (info.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(StorePickReserveAct.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(StorePickReserveAct.this, info.getErrorMsg());
                    }
                } else {
                    Message msg = new Message();
                    msg.what = GET_EXTRACT_GOLD_TYPE_SUCCESS;
                    msg.obj = info;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void showOrderDetailView() {
        OrderPopupView orderPopupView = new OrderPopupView(StorePickReserveAct.this, orderInfo.getFee(), "3",
                orderInfo);
        orderPopupView.showAtLocation(root, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
        ActivityUtils.backgroundAlpha(StorePickReserveAct.this, 0.5f);
        orderPopupView.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ActivityUtils.backgroundAlpha(StorePickReserveAct.this, 1f);
            }
        });
    }

    private void submitExtractReseverInfo() {
        getGramList();
        ActionUtil actionUtil = new ActionUtil(StorePickReserveAct.this);
        actionUtil.submitExtractReseverInfo(shopId, reseverTime, isBill, goldType, gramList);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                OrderInfo info = (OrderInfo) model;
                if (info == null) {
                    UIHelper.showToast(StorePickReserveAct.this, getString(R.string.rank_fail));
                    return;
                }
                if (info.getErrorMsg() != null) {
                    if (info.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(StorePickReserveAct.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(StorePickReserveAct.this, info.getErrorMsg());
                    }
                } else {
                    if ("0".equals(info.getStatus())) {
                        UIHelper.showToast(StorePickReserveAct.this, info.getMsg());
                    } else {
                        Message msg = new Message();
                        msg.what = SUBMIT_EXTRACT_RESEVER_INFO_SUCCESS;
                        msg.obj = info;
                        handle.sendMessage(msg);
                    }
                }
            }
        });
    }

    private void getGramList() {
        gramList = new ArrayList<>();
        ExtractGold gram20Gold = new ExtractGold();
        gram20Gold.setId(20);
        gram20Gold.setCount(gram20);
        ExtractGold gram50Gold = new ExtractGold();
        gram50Gold.setId(50);
        gram50Gold.setCount(gram50);
        ExtractGold gram100Gold = new ExtractGold();
        gram100Gold.setId(100);
        gram100Gold.setCount(gram100);
        ExtractGold gram200Gold = new ExtractGold();
        gram200Gold.setId(200);
        gram200Gold.setCount(gram200);
        ExtractGold gram500Gold = new ExtractGold();
        gram500Gold.setId(500);
        gram500Gold.setCount(gram500);
        ExtractGold gram1000Gold = new ExtractGold();
        gram1000Gold.setId(1000);
        gram1000Gold.setCount(gram1000);
        gramList.add(gram20Gold);
        gramList.add(gram50Gold);
        gramList.add(gram100Gold);
        gramList.add(gram200Gold);
        gramList.add(gram500Gold);
        gramList.add(gram1000Gold);
    }

    @OnClick({R.id.iv_left_arrow, R.id.locate_layout, R.id.iv_call, R.id.rl_goldtype_layout, R.id.rl_resever_time_layout, R.id.switch_btn_invoice, R.id.btn_submit_reserve})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_arrow:
                finish();
                break;
            case R.id.locate_layout:
                if (info != null) {
                    ActivityUtils.gotoShopPosition(StorePickReserveAct.this, info.getAddress(), info.getPoint().getX(),
                            info.getPoint().getY(), info.getShopName());
                }
                break;
            case R.id.iv_call:
                if (info != null) {
                    UIHelper.showCallRemindDialog(StorePickReserveAct.this, info.getShopPhone());
                }
                break;
            case R.id.rl_goldtype_layout:
                ReseverTimePopubView typePopupView = new ReseverTimePopubView(StorePickReserveAct.this,
                        StorePickReserveAct.this, gold_type);
                typePopupView.showAtLocation(root, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                ActivityUtils.backgroundAlpha(StorePickReserveAct.this, 0.5f);
                typePopupView.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        ActivityUtils.backgroundAlpha(StorePickReserveAct.this, 1f);
                    }
                });
                typePopupView.setValueSetClickListener(new Interface.OnValueSetClickListener() {

                    @Override
                    public void onValueSet(String value) {
                        tvGoldType.setText(value);
                        goldType = value;
                    }
                });
                break;
            case R.id.rl_resever_time_layout:
                pvTime.show();
                break;
            case R.id.switch_btn_invoice:
                if (isInvoiceToogle) {
                    switchBtnInvoice.setSelected(false);
                    isInvoiceToogle = false;
                    isBill = "1";
                    tvInvoice.setText("不要发票");
                } else {
                    switchBtnInvoice.setSelected(true);
                    isInvoiceToogle = true;
                    isBill = "0";
                    tvInvoice.setText("需要发票");
                }
                break;
            case R.id.btn_submit_reserve:
                reseverTime = tvReseverTime.getText().toString().trim();
                if (TextUtils.isEmpty(reseverTime)) {
                    UIHelper.showToast(StorePickReserveAct.this, "请选择预约时间!");
                    return;
                }
                submitExtractReseverInfo();
                break;
        }
    }
}
